package org.light.jpa.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.domain.Type;
import org.light.domain.Var;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.generator.NamedS2SMStatementGenerator;
import org.light.generator.NamedS2SMStatementListGenerator;
import org.light.generator.NamedStatementGenerator;
import org.light.limitedverb.CountActiveRecords;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class ToggleOne extends org.light.verb.ToggleOne {

	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("toggleOne" + StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("Boolean"));
			method.setThrowException(true);
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken() + "." + this.domain.getDomainSuffix() + "."
					+ this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1, this.domain.getDomainId().getFieldName(),
					this.domain.getDomainId().getClassType()));
			return method;
		}
	}

	@Override
	public Method generateServiceImplMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("toggleOne" + StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("Boolean"));
			method.setThrowException(true);
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken() + "." + this.domain.getDomainSuffix() + "."
					+ this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken() + "." + this.domain.getDaoSuffix() + "."
					+ this.domain.getStandardName() + "Dao");
			method.addAdditionalImport(this.domain.getPackageToken() + "." + this.domain.getServiceSuffix() + "."
					+ this.domain.getStandardName() + "Service");
			method.addSignature(new Signature(1, this.domain.getDomainId().getFieldName(),
					this.domain.getDomainId().getClassType()));
			method.addMetaData("Override");

			// Service method
			FindById find = new FindById(this.domain);
			Toggle toggle = new Toggle(this.domain);
			CountActiveRecords countActive = new CountActiveRecords(this.domain);

			List<Writeable> list = new ArrayList<Writeable>();
			list.add(new Statement(100L, 2,
					this.domain.getCapFirstDomainNameWithSuffix() + " " + this.domain.getLowerFirstDomainName()
							+ " = dao." + StringUtil.lowerFirst(find.getVerbName()) + "("
							+ this.domain.getDomainId().getLowerFirstFieldName() + ");"));
			if ("oracle".equalsIgnoreCase(this.dbType)) {
				list.add(new Statement(200L, 2,
						"if (" + this.domain.getLowerFirstDomainName() + ".get"
								+ this.domain.getActive().getCapFirstFieldName() + "()=="
								+ this.domain.getDomainDeletedInteger() + ") {"));

			}else {
				list.add(new Statement(200L, 2,
						"if (" + this.domain.getLowerFirstDomainName() + ".get"
								+ this.domain.getActive().getCapFirstFieldName() + "()=="
								+ this.domain.getDomainDeletedStr() + ") {"));
			}
						
			list.add(new Statement(300L, 3, "dao." + StringUtil.lowerFirst(toggle.getVerbName()) + "("
					+ this.domain.getDomainId().getLowerFirstFieldName() + ");"));
			list.add(new Statement(400L, 2, "}"));
			list.add(new Statement(500L, 2, "else {"));
			list.add(new Statement(600L, 3,
					"Integer count = dao." + StringUtil.lowerFirst(countActive.getVerbName()) + "();"));
			list.add(new Statement(700L, 3, "if (count > 1){"));
			list.add(new Statement(800L, 4, "dao." + StringUtil.lowerFirst(toggle.getVerbName()) + "("
					+ this.domain.getDomainId().getLowerFirstFieldName() + ");"));
			list.add(new Statement(900L, 3, "}"));
			list.add(new Statement(1000L, 2, "}"));
			list.add(new Statement(1100L, 2, "return true;"));
			method.setMethodStatementList(WriteableUtil.merge(list));
			return method;
		}
	}

	public ToggleOne() {
		super();
		this.setLabel("留一切换");
	}

	public ToggleOne(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		this.denied = domain.isVerbDenied("ToggleOne");
		this.setVerbName("ToggleOne" + StringUtil.capFirst(this.domain.getStandardName()));
		this.setLabel("留一切换");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("ToggleOne");
	}
}
